package datalookin.adapter2;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.whu.zj.MyApplication;
import com.whu.zj.R;

import java.util.ArrayList;
import java.util.List;

import datalookin.dto2.WaterPlantDataDto;
import main.dto.WaterPlant;

/**
 * Created by Administrator on 2016/6/5.
 */
public class RealPlantAdapter extends BaseAdapter {

    List<KV> kvs = new ArrayList<KV>();
    List<KV2> kv2s = new ArrayList<KV2>();

    WaterPlantDataDto waterPlantDataDtos = new WaterPlantDataDto();

    public void setWaterPlantDataDtos(WaterPlantDataDto waterPlantDataDtos) {
        this.waterPlantDataDtos = waterPlantDataDtos;
        initkvs();
        initkv2s();
    }

    private void initkv2s() {
        for (int i=0;i<kvs.size();i++)
        {
            if (i%2 == 0)
            {
                KV2 kv2 = new KV2();
                kv2.setKv1(kvs.get(i));
                if (i+1<=kvs.size()-1)
                    kv2.setKv2(kvs.get(i+1));
                kv2s.add(kv2);
            }
        }
    }

    private void initkvs() {
        WaterPlant nowPlant = MyApplication.getRealPlant();


        WaterPlantDataDto waterPlantDataDto = waterPlantDataDtos;
        kvs.add(new KV("原水PH",waterPlantDataDto.getPh()));
        kvs.add(new KV("原水浊度(NTU)",waterPlantDataDto.getTurbidity()));
        kvs.add(new KV("压力(KPA)",waterPlantDataDto.getPressureValue()));
        kvs.add(new KV("电流(A)",waterPlantDataDto.getaElectric()));
        kvs.add(new KV("电压(V)",waterPlantDataDto.getVoltage()));
        kvs.add(new KV("功率(KW)",waterPlantDataDto.getGonglv()));
        kvs.add(new KV("电量(Kwh)",waterPlantDataDto.getPower()));
        kvs.add(new KV("清水池液位(m)",waterPlantDataDto.getWaterLevel()));
        for (int i=0;i< nowPlant.getSupplyPumpNum();i++)
        {
            switch (i)
            {
                case 0:
                    kvs.add(new KV("一号供水泵状态",zhuangtaiHY(waterPlantDataDto.getSupplyState1())));
                    kvs.add(new KV("一号供水泵运行频率(HZ)",waterPlantDataDto.getFrequency1()));
                    kvs.add(new KV("一号供水泵转速(r/m)",waterPlantDataDto.getRotatingSpeed1()));
                    break;
                case 1:
                    kvs.add(new KV("二号供水泵状态",zhuangtaiHY(waterPlantDataDto.getSupplyState2())));
                    kvs.add(new KV("二号供水泵运行频率(HZ)",waterPlantDataDto.getFrequency2()));
                    kvs.add(new KV("二号供水泵转速(r/m)",waterPlantDataDto.getRotatingSpeed2()));
                    break;
                case 2:

                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
                case 6:
                    break;
                case 7:
                    break;

            }
        }

        for (int i=0;i<nowPlant.getSupplyFlowNum();i++)
        {
            String flowName = "";
            switch (i)
            {
                case 0:
                    flowName = MyApplication.mobileUserDto.getNametoname().getFlowmeter1();
                    kvs.add(new KV(flowName+"瞬时流量(M³/H)",waterPlantDataDto.getFlow1()));
                    kvs.add(new KV(flowName+"供水量(M³)",waterPlantDataDto.getSupplyFlow1()));
                    break;
                case 1:
                    flowName = MyApplication.mobileUserDto.getNametoname().getFlowmeter2();
                    kvs.add(new KV(flowName+"瞬时流量(M³/H)",waterPlantDataDto.getFlow2()));
                    kvs.add(new KV(flowName+"供水量(M³)",waterPlantDataDto.getSupplyFlow2()));
                    break;
                case 2:
                    flowName = MyApplication.mobileUserDto.getNametoname().getFlowmeter3();
                    kvs.add(new KV(flowName+"瞬时流量(M³/H)",waterPlantDataDto.getFlow3()));
                    kvs.add(new KV(flowName+"供水量(M³)",waterPlantDataDto.getSupplyFlow3()));
                    break;
                case 3:
                    flowName = MyApplication.mobileUserDto.getNametoname().getFlowmeter4();
                    kvs.add(new KV(flowName+"瞬时流量(M³/H)",waterPlantDataDto.getFlow4()));
                    kvs.add(new KV(flowName+"供水量(M³)",waterPlantDataDto.getSupplyFlow4()));
                    break;
                case 4:
                    flowName = MyApplication.mobileUserDto.getNametoname().getFlowmeter5();
                    kvs.add(new KV(flowName+"瞬时流量(M³/H)",waterPlantDataDto.getFlow5()));
                    kvs.add(new KV(flowName+"供水量(M³)",waterPlantDataDto.getSupplyFlow5()));
                    break;
                case 5:
                    flowName = MyApplication.mobileUserDto.getNametoname().getFlowmeter6();
                    kvs.add(new KV(flowName+"瞬时流量(M³/H)",waterPlantDataDto.getFlow6()));
                    kvs.add(new KV(flowName+"供水量(M³)",waterPlantDataDto.getSupplyFlow6()));
                    break;
                case 6:
                    break;
            }
        }

        kvs.add(new KV("总供水量(M³)",waterPlantDataDto.getTotalSupplyFlow()));
        kvs.add(new KV("出厂余氯(mg/L)",waterPlantDataDto.getChlorineOut()));
        kvs.add(new KV("出厂浊度(NTU)",waterPlantDataDto.getTurbidityOut()));

        for (int i=0;i<nowPlant.getGetPumpNum();i++)
        {
            switch (i)
            {
                case 0:
                    kvs.add(new KV("一号取水泵状态",zhuangtaiHY(waterPlantDataDto.getGetState1())));
                    break;
                case 1:
                    kvs.add(new KV("二号取水泵状态",zhuangtaiHY(waterPlantDataDto.getGetState2())));
                    break;
                case 2:
                    kvs.add(new KV("三号取水泵状态",zhuangtaiHY(waterPlantDataDto.getGetState3())));
                    break;
                case 3:
                    kvs.add(new KV("四号取水泵状态",zhuangtaiHY(waterPlantDataDto.getGetState4())));
                    break;
                case 4:
                    break;
                case 5:
                    break;

            }
        }

        for (int i=0;i<nowPlant.getBackwashFanNum();i++)
        {
            switch (i)
            {
                case 0:
                    kvs.add(new KV("一号反冲洗风机状态",zhuangtaiHY(waterPlantDataDto.getBackwashFan1())));
                    break;
                case 1:
                    kvs.add(new KV("二号反冲洗风机状态",zhuangtaiHY(waterPlantDataDto.getBackwashFan2())));
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
                case 5:
                    break;
            }
        }

        for (int i=0;i<nowPlant.getBackwashPumpNum();i++)
        {
            switch (i)
            {
                case 0:
                    kvs.add(new KV("一号反冲洗水泵状态",zhuangtaiHY(waterPlantDataDto.getBackwashPump1())));
                    break;
                case 1:
                    kvs.add(new KV("二号反冲洗水泵状态",zhuangtaiHY(waterPlantDataDto.getBackwashPump2())));
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;

            }
        }

        kvs.add(new KV("出厂水PH",waterPlantDataDto.getOutPH()));
        kvs.add(new KV("取水流量(M³/H)",waterPlantDataDto.getGetFlow()));
    }

    String zhuangtaiHY(String status)
    {
        if (status.equals("-1"))
        {
            return "关";
        }
        else
        {
            return "开";
        }
    }


    @Override
    public int getCount() {

        int remain =  kvs.size()%2;
        int lines = remain==0? kvs.size()/2:kvs.size()/2+1;
        return lines;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder viewHolder = null;
        if (convertView == null)
        {
            convertView = View.inflate(MyApplication.share(), R.layout.item_realdata,null);
            viewHolder = new ViewHolder();
            viewHolder.name1 = (TextView) convertView.findViewById(R.id.tv_name1);
            viewHolder.value1 = (TextView) convertView.findViewById(R.id.tv_value1);
            viewHolder.name2 = (TextView) convertView.findViewById(R.id.tv_name2);
            viewHolder.value2 = (TextView) convertView.findViewById(R.id.tv_value2);
            convertView.setTag(viewHolder);
        }
        else
        {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        KV2 kv2 = kv2s.get(i);
        viewHolder.name1.setText(kv2.getKv1().key);
        viewHolder.value1.setText(kv2.getKv1().value);
        if (kv2.getKv2() != null)
        {
            viewHolder.name2.setText(kv2.getKv2().key);
            viewHolder.value2.setText(kv2.getKv2().value);
        }


        return convertView;
    }

    class ViewHolder
    {
        TextView name1;
        TextView value1;
        TextView name2;
        TextView value2;
    }

    class KV{
        String key;
        String value;

        public KV(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }

    class KV2{
        KV kv1;
        KV kv2;

        public KV getKv1() {
            return kv1;
        }

        public void setKv1(KV kv1) {
            this.kv1 = kv1;
        }

        public KV getKv2() {
            return kv2;
        }

        public void setKv2(KV kv2) {
            this.kv2 = kv2;
        }
    }
}
